package com.gee.spring.cloud.data.transform.core.center.config.handler;

import com.gee.spring.cloud.common.core.entity.center.config.Org;
import com.gee.spring.cloud.common.starter.config.event.CenterConfigEventHandler;
import com.gee.spring.cloud.common.starter.feign.center.config.OrgClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author gepengjun
 * @since 2024/3/19 14:38
 */
@Component
public class OrgHandler extends CenterConfigEventHandler<Org> {

    private final ConcurrentHashMap<Long, Org> ORG_MAP = new ConcurrentHashMap<>();

    @Autowired
    private OrgClient orgClient;

    public Org getById(Long id){
        return ORG_MAP.computeIfAbsent(id, orgId -> orgClient.getById(id).getData());
    }

    @Override
    public void handlerUpdate(List<Org> dataList, List<Org> oldList) {
        dataList.forEach(org -> ORG_MAP.computeIfPresent(org.getId(), (id, old) -> org));
    }

    @Override
    public void handlerDelete(List<Org> oldList) {
        oldList.forEach(org -> ORG_MAP.remove(org.getId()));
    }

}
